package com.weavernorth.create;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.logging.Log;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.workflow.webservices.WorkflowRequestTableField;
import weaver.workflow.webservices.WorkflowRequestTableRecord;

public class DetailFamily {


    private static Log log = org.apache.commons.logging.LogFactory.getLog(DetailFamily.class.getName());
    BaseBean bb =new BaseBean();
    public WorkflowRequestTableRecord[] detailFamily(JSONArray familyJsonarry) {
        WorkflowRequestTableRecord[] wrtri = new WorkflowRequestTableRecord[familyJsonarry.size()];
        log.info("家庭成员：" + familyJsonarry.size());

        if (familyJsonarry!=null)   {
            WorkflowRequestTableField[] wrti;
            wrtri = new WorkflowRequestTableRecord[familyJsonarry.size()];
            //同一明细表可能存在多个，所以循环处理
            for (int i = 0; i < familyJsonarry.size(); i++) {
                JSONObject jsonObject = JSONObject.fromObject(familyJsonarry.get(i));

                //添加成员关系字段
                //从JSONobject中获取字段信息
                String strCygx = String.valueOf(jsonObject.get("relationship"));
                //数组有5个字段长度为5
                wrti = new WorkflowRequestTableField[5];
                wrti[0] = new WorkflowRequestTableField();
                //字段在数据库中的字段名
                wrti[0].setFieldName("cygx");
                //查询对应码表的值
                String cygxStr = " select cygx from uf_cygx where cygxdmdydm = '"+strCygx+"'";
                RecordSet rscygx  =  new RecordSet();
                rscygx.execute(cygxStr);
                if(rscygx.next()){
                    wrti[0].setFieldValue(rscygx.getString("cygx"));
                }
                else{
                    //赋值
                    wrti[0].setFieldValue(strCygx);
                }
                wrti[0].setView(true);
                wrti[0].setEdit(true);

                //添加姓名字段
                //从JSONobject中获取字段信息
                String strMxxm = String.valueOf(jsonObject.get("name"));
                wrti[1] = new WorkflowRequestTableField();
                //字段在数据库中的字段名
                wrti[1].setFieldName("xm");
                //赋值
                wrti[1].setFieldValue(strMxxm);
                wrti[1].setView(true);
                wrti[1].setEdit(true);

                //添加联系方式字段
                //从JSONobject中获取字段信息
                String strLxfs = String.valueOf(jsonObject.get("ContactInformation"));
                wrti[2] = new WorkflowRequestTableField();
                //字段在数据库中的字段名
                wrti[2].setFieldName("lxfs");
                //赋值
                wrti[2].setFieldValue(strLxfs);
                wrti[2].setView(true);
                wrti[2].setEdit(true);

                //添加现工作单位字段
                //从JSONobject中获取字段信息
                String strXgzdw = String.valueOf(jsonObject.get("company"));
                wrti[3] = new WorkflowRequestTableField();
                //字段在数据库中的字段名
                wrti[3].setFieldName("xgzdw");
                //赋值
                wrti[3].setFieldValue(strXgzdw);
                wrti[3].setView(true);
                wrti[3].setEdit(true);


                //添加性别字段
                //从JSONobject中获取字段信息
                String strCygxxb = String.valueOf(jsonObject.get("Sex"));
                log.info("性别："+strCygxxb);
                wrti[4] = new WorkflowRequestTableField();
                //字段在数据库中的字段名
                wrti[4].setFieldName("xb");
                log.info(strCygxxb + "性别");
                //赋值
                //码表关系转换
                if("0/599/604".equals(strCygxxb)){
                    String boy = bb.getPropValue("porturl","boy");
                    wrti[1].setFieldValue(Util.null2String(boy));
                }
                if("0/599/605".equals(strCygxxb)){
                    String girl = bb.getPropValue("porturl","girl");
                    wrti[4].setFieldValue(Util.null2String(girl));
                }
                wrti[4].setView(true);
                wrti[4].setEdit(true);
                wrti[4].setFieldOrder(0);
                wrti[4].setMand(false);



                wrtri[i] = new WorkflowRequestTableRecord();
                wrtri[i].setWorkflowRequestTableFields(wrti);
            }
        }



        return wrtri;

    }


}
